package org.dawning.shop.category.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.dawning.shop.category.service.BaseCategoryService;
import org.dawning.shop.core.base.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by L on 2017/6/12.
 * 基础分类的接口类
 */
@RestController
@RequestMapping("/base/category")
public class BaseCategoryController {

    @Autowired
    private BaseCategoryService categoryService;

    @ApiOperation("添加分类接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "pid",value = "父分类ID，可为空",required = false,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "name",value = "分类名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "code",value = "分类编码",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "sort",value = "排序",paramType = "query",dataType = "int")
    })
    @ApiResponse(code=200,message = "返回分类信息")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResult add(@RequestParam String name, @RequestParam(required = false) String code, @RequestParam(required = false) String pid, @RequestParam(required = false) int sort){
        return categoryService.addCategory(pid,name,code,sort);
    }

    @ApiOperation("删除分类接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "分类ID",required = true,paramType = "query",dataType = "String")
    })
    @ApiResponse(code=200,message = "返回是否成功")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    public BaseResult delete(@PathVariable String id){
        return categoryService.deleteCategory(id);
    }

    @ApiOperation("获取分类及父分类接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "分类ID",required = true,paramType = "query",dataType = "String")
    })
    @ApiResponse(code=200,message = "返回分类信息")
    @RequestMapping(value = "/get/{id}",method = RequestMethod.POST)
    public BaseResult get(@PathVariable String id){
        return categoryService.getCategory(id);
    }


    @ApiOperation("更新分类接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "pd",value = "分类ID",required = false,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "name",value = "分类名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "code",value = "分类编码",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "sort",value = "排序",paramType = "query",dataType = "int")
    })
    @ApiResponse(code=200,message = "返回分类信息")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public BaseResult update(@RequestParam String id,@RequestParam String name,@RequestParam(required = false) String code,@RequestParam(required = false) int sort){
        return categoryService.updateCategory(id,name,code,sort);
    }



}